<?php
require_once(ROOT . LIBRARY .'/fields/common.inc');

class fields_types extends fields_common implements field
{

   public function  __construct($data)
   {
      parent::__construct($data);
      $this->escaped_columns[] = 'values';
      $this->escaped_columns[] = 'src_value';

      $this->values = kernel::getInstance()->getTypes($this->tp_belong);
   }

   
   /**
    * Normalize Value
    *
    * @param string $value
    * @param string $lang
    */
   public function normalizeValue($value, $lang = null)
   {
      $this->src_value = explode(',', $value);
      $value = array();
      
      foreach($this->src_value AS $k=>$v)
      {
         if(isset($this->values[$v]))
         {
            $value[$k] = $this->values[$v];
         } elseif(($tmp = array_search($v, $this->values)) !== false)
         {
            $value[$tmp] = $v;
         }
      }

      $this->src_value = array_keys($value);
      $this->value = array_values($value);
      return $this->value;
   }


   /**
    * Get HTML code for Types fields
    *
    * @param object $data
    * @return string
    */
   public function getFieldHTML($data)
   {
      $alias = !empty($this->db) ? $this->db : $this->alias;
      $value = is_object($data) ? $data->{$alias}(null) : NULL;
      $KERNEL = kernel::getInstance();
      $types = $KERNEL->getTypes($this->tp_belong);
      $html  = '';

      if($types)
      {
         $html = '<input type="hidden" name="f['. $this->alias .']';
         if(!empty($this->fieldset)) $html .= '['. (!empty($data->id) ? $data->id : $this->fieldset) .']';
         $html .= '[]" value="" />';
         $html .= '<ul class="types_list">'.EOL;

         foreach($types AS $type_key=>$type_name)
         {
            $html .= '<li><input type="checkbox" value="'. $type_key .'" name="f['. $this->alias .']';
            if(!empty($this->fieldset)) $html .= '['. (!empty($data->id) ? $data->id : $this->fieldset) .']';
            $html .= '[]" id="f_'. $this->alias .'_'. $type_key .'"';
            if(in_array($type_name, (array)$value)) $html .= ' checked="checked"';
            $html .= ' />'.EOL;
            $html .= '<label for="f_'. $this->alias .'_'. $type_key .'"> - ' . $type_name . '</label></li>'.EOL;
         }

         $html .= '</ul>';
      }

      return $html;
   }


   public function validateInput($value, $objectData = NULL)
   {
      $this->value = array();
      if(!empty($value) && is_string($value)) $value = explode(',', $value);

      if($value)
      {
         foreach((array)$value AS $type)
         {
            if(!empty($this->values))
            {
               if(isset($this->values[$type]))
               {
                  $this->value[] = $type;
               } elseif(($i = array_search($type, $this->values)) !== false)
               {
                  $this->value[] = $i;
               }
            }
         }
      }

      return $this->value;
   }

   public function saveValue($lang = NULL)
   {
      if(property_exists($this, 'value'))
      {
         return join(',', $this->value);
      }

      return '';
   }
}
